import 'package:flutter/material.dart';

import '../../config/public_color.dart';
import '../../config/public_text.dart';
import '../../foundation/style/bold_text_style.dart';
import '../button/border_button.dart';
import '../button/public_button.dart';
import '../container/separated_row.dart';

/// FileName public_alert_dialog
///
/// @Author lax
/// @Date 2024/4/3
///
/// @Description

class PublicAlertDialog extends AlertDialog {
  static Future<bool?> show({
    required BuildContext context,
    String? title,
    String? message,
    Widget? contentWidget,
    String? confirmText,
    String? cancelText,
    TextStyle? titleStyle,
    TextStyle? messageStyle,
    TextStyle? confirmTextStyle,
    TextStyle? cancelTextStyle,
    Color? confirmColor,
    List<Color>? confirmColors,
    Color? cancelColor,
    EdgeInsets? titlePadding,
    EdgeInsets? contentPadding,
    EdgeInsets? actionsPadding,
    Function? onConfirm,
    Function? onCancel,
    bool showCancel = true,
    bool clickButtonDismiss = true,
    bool clickBarrierDismiss = true,
  }) {
    builder(context) {
      return PublicAlertDialog(
        title: title,
        message: message,
        contentWidget: contentWidget,
        confirmText: confirmText,
        cancelText: cancelText,
        titleStyle: titleStyle,
        messageStyle: messageStyle,
        confirmTextStyle: confirmTextStyle,
        cancelTextStyle: cancelTextStyle,
        confirmColor: confirmColor,
        confirmColors: confirmColors,
        cancelColor: cancelColor,
        titlePadding: titlePadding,
        contentPadding: contentPadding,
        actionsPadding: actionsPadding,
        onConfirm: onConfirm,
        onCancel: onCancel,
        showCancel: showCancel,
        clickButtonDismiss: clickButtonDismiss,
        clickBlankDismiss: clickBarrierDismiss,
      );
    }

    return showDialog<bool>(
      barrierDismissible: clickBarrierDismiss,
      context: context,
      builder: builder,
    );
  }

  PublicAlertDialog({
    super.key,
    String? title,
    String? message,
    Widget? contentWidget,
    String? confirmText,
    String? cancelText,
    TextStyle? titleStyle,
    TextStyle? messageStyle,
    TextStyle? confirmTextStyle,
    TextStyle? cancelTextStyle,
    Color? confirmColor,
    List<Color>? confirmColors,
    Color? cancelColor,
    EdgeInsets? titlePadding,
    EdgeInsets? contentPadding,
    EdgeInsets? actionsPadding,
    Function? onConfirm,
    Function? onCancel,
    bool showCancel = true,
    bool clickButtonDismiss = true,
    bool clickBlankDismiss = true,
  }) : super(
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(8.0)),
          ),
          backgroundColor: PublicColor.background,
          titlePadding: titlePadding ??
              const EdgeInsets.only(top: 20, left: 20, right: 20),
          contentPadding: contentWidget == null && message == null
              ? EdgeInsets.zero
              : (contentPadding ??
                  EdgeInsets.only(
                      top: (title == null || title == '') ? 20 : 16,
                      left: 20,
                      right: 20)),
          iconPadding: EdgeInsets.zero,
          buttonPadding: EdgeInsets.zero,
          actionsPadding: actionsPadding ??
              const EdgeInsets.only(top: 16, left: 20, right: 20, bottom: 20),
          title: title == null
              ? null
              : Text(
                  title,
                  textAlign: TextAlign.center,
                  style: titleStyle ?? BoldTextStyle(fontSize: 16),
                ),
          content: Visibility(
            visible: contentWidget == null,
            replacement: Column(
              mainAxisSize: MainAxisSize.min,
              children: [contentWidget ?? const SizedBox()],
            ),
            child: message == null
                ? const SizedBox()
                : Text(
                    message,
                    textAlign: TextAlign.center,
                    style: messageStyle ??
                        BoldTextStyle(
                            color: PublicColor.darkText, fontSize: 14),
                  ),
          ),
          actions: [
            Builder(
              builder: (context) => SeparatedRow(
                separator: const SizedBox(width: 12),
                expanded: true,
                children: [
                  Visibility(
                    visible: showCancel,
                    child: BorderButton(
                      text: cancelText ?? PublicText.cancel,
                      textStyle: cancelTextStyle ??
                          BoldTextStyle(
                              color: cancelColor ?? PublicColor.primary),
                      onPressed: () {
                        if (clickButtonDismiss) {
                          Navigator.of(context).pop();
                        }
                        onCancel?.call();
                      },
                    ),
                  ),
                  PublicButton(
                    text: confirmText ?? PublicText.confirm,
                    textStyle: confirmTextStyle ??
                        BoldTextStyle(color: Colors.white),
                    color: confirmColor,
                    colors: confirmColors,
                    onPressed: () {
                      if (clickButtonDismiss) {
                        Navigator.of(context).pop();
                      }
                      onConfirm?.call();
                    },
                  ),
                ],
              ),
            ),
          ],
        );
}
